package Test1.Thermometry;

import Test1.HCNetSDK;
import com.sun.jna.NativeLong;

import javax.swing.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by panting6 on 2021/2/21.
 */
public class JpegWithAppendData {
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    public void jpegWithAppendData(int lUserID, int iChannelNum)
    {
        boolean bRet = false;
        HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA   m_strJpegWithAppendData = new HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA();
        m_strJpegWithAppendData.dwSize = m_strJpegWithAppendData.size();
        HCNetSDK.BYTE_ARRAY ptrJpegByte = new HCNetSDK.BYTE_ARRAY(4 * 1024 *1024);
        HCNetSDK.BYTE_ARRAY ptrP2PDataByte = new HCNetSDK.BYTE_ARRAY(2 * 1024 *1024);
        HCNetSDK.BYTE_ARRAY ptrVisiblePicByte = new HCNetSDK.BYTE_ARRAY(40*1024*1024);
        m_strJpegWithAppendData.pJpegPicBuff =ptrJpegByte.getPointer();
        m_strJpegWithAppendData.pP2PDataBuff = ptrP2PDataByte.getPointer();
        m_strJpegWithAppendData.pVisiblePicBuff = ptrVisiblePicByte.getPointer();
        m_strJpegWithAppendData.dwJpegPicLen = 4 * 1024 *1024;
        m_strJpegWithAppendData.dwP2PDataLen = 2 * 1024 *1024;
        m_strJpegWithAppendData.dwVisiblePicLen = 40 * 1024 *1024;
        m_strJpegWithAppendData.write();

        bRet=hCNetSDK.NET_DVR_CaptureJPEGPicture_WithAppendData(lUserID,iChannelNum,m_strJpegWithAppendData);

        if(bRet==true)
        {
            m_strJpegWithAppendData.read();
            System.out.println("NET_DVR_CaptureJPEGPicture_WithAppendData调用成功！图片和温度数据已保存在pic文件夹中，热成像有效区域：{"
                    + m_strJpegWithAppendData.struThermalValidRect.fX + "," + m_strJpegWithAppendData.struThermalValidRect.fY
                    + "," + m_strJpegWithAppendData.struThermalValidRect.fWidth + "," + m_strJpegWithAppendData.struThermalValidRect.fHeight + "},可见光有效区域：{"
                    + m_strJpegWithAppendData.struVisibleValidRect.fX + "," + m_strJpegWithAppendData.struVisibleValidRect.fY + "," + m_strJpegWithAppendData.struVisibleValidRect.fWidth
                    + "," + m_strJpegWithAppendData.struVisibleValidRect.fHeight + "}");
            //测温数据
            if (m_strJpegWithAppendData.dwP2PDataLen > 0)
            {
                SimpleDateFormat sf = new SimpleDateFormat("yyyymmddHHmmss");
                String newName = sf.format(new Date());
                File dir = new File(".\\pic\\");
                if (!dir.exists()){
                    dir.mkdir();
                }
                FileOutputStream fout;
                BufferedWriter fout1;
                try {
                        fout = new FileOutputStream(".\\pic\\" + "测温" +newName+ ".data");
                        ByteBuffer bufferss = m_strJpegWithAppendData.pP2PDataBuff.getByteBuffer(0, m_strJpegWithAppendData.dwP2PDataLen);
                        byte[] bytess = new byte[m_strJpegWithAppendData.dwP2PDataLen];
                        bufferss.rewind();
                        bufferss.get(bytess);
                        fout.write(bytess);
                        fout.close();
                    fout1 = new BufferedWriter(new FileWriter(dir+ "\\测温" + newName + ".csv"));
                    String tempdata ="";
                    byte[] byTempData = new byte[4];
                    for (int i = 0; i < m_strJpegWithAppendData.dwJpegPicHeight; i++)
                    {
                        for (int j = 0; j < m_strJpegWithAppendData.dwJpegPicWidth; j++)
                        {
                            ByteBuffer TempDatabuffers = m_strJpegWithAppendData.pP2PDataBuff.getByteBuffer((j +i*m_strJpegWithAppendData.dwJpegPicWidth) * 4 , 4);
                            TempDatabuffers.get(byTempData);
                            int l;
                            l = byTempData[0];
                            l &= 0xff;
                            l |= ((long) byTempData[1] << 8);
                            l &= 0xffff;
                            l |= ((long) byTempData[2] << 16);
                            l &= 0xffffff;
                            l |= ((long) byTempData[3] << 24);
                            //System.out.println("行数：" + i + "列数：" + j + "温度数据：" + Float.intBitsToFloat(l));
                            tempdata =" " + Float.intBitsToFloat(l);
                            fout1.write(tempdata);
                            fout1.write(",");
                        }fout1.newLine();
                    }fout1.close();
                } catch (FileNotFoundException e) {
                    System.out.println("路径不存在");
                } catch (IOException e) {
                    System.out.println("异常");
                }
            }
            //测温图片
            if (m_strJpegWithAppendData.dwJpegPicLen > 0)
            {
                SimpleDateFormat sf = new SimpleDateFormat("yyyymmddHHmmss");
                String newName = sf.format(new Date());
                FileOutputStream pic;
                FileOutputStream pic1;
                FileOutputStream pic2;
                try {
                    pic = new FileOutputStream(".\\pic\\" + "测温_T"+newName + ".jpg");
                    pic1 = new FileOutputStream(".\\pic\\" + "测温_T"+newName + ".data");
                    pic2 = new FileOutputStream(".\\pic\\" + "测温_V"+newName + ".jpg");
                    ByteBuffer buffers = m_strJpegWithAppendData.pJpegPicBuff.getByteBuffer(0, m_strJpegWithAppendData.dwJpegPicLen);
                    ByteBuffer buffers1 = m_strJpegWithAppendData.pVisiblePicBuff.getByteBuffer(0, m_strJpegWithAppendData.dwVisiblePicLen);
                    byte[] bytes = new byte[m_strJpegWithAppendData.dwJpegPicLen];
                    byte[] bytes1 = new byte[m_strJpegWithAppendData.dwVisiblePicLen];
                    buffers.rewind();
                    buffers.get(bytes);
                    buffers1.rewind();
                    buffers1.get(bytes1);
                    pic.write(bytes);
                    pic1.write(bytes);
                    pic2.write(bytes1);
                    pic.close();
                    pic1.close();
                    pic2.close();

                } catch (FileNotFoundException e) {
                    System.out.println("路径不存在");
                } catch (IOException e) {
                    System.out.println("异常");
                }
            }
        }
        else
        {
            System.out.println( "抓图叠加温度信息失败，错误码"+ hCNetSDK.NET_DVR_GetLastError());
        }
    }
}
